package com.wonders.frame.common;


import org.apache.commons.codec.digest.DigestUtils;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

public class SecretUtil {
    public static final String salt = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC2vRf2Gn3MOcf4y4vJQyu4o69+jZubaJeufx9MuCSoTD6b3pyvp1H+hQk0sn6okc3ClT5gXxkmjwZm7/J9ZDSAuZhkrTgTg3+CjEEVBMJPabcqz8csROwY5KRgRDrf4mUXt34xKSoXdPwAIxJnQuNSoTi+SX2HpBD0FTbFMiFwfwIDAQAB";

    public static String getAuthentication(String str){
        return DigestUtils.md5Hex(str + salt);
    }

    /*
     * 加密
     * 1.构造密钥生成器
     * 2.根据ecnodeRules规则初始化密钥生成器
     * 3.产生密钥
     * 4.创建和初始化密码器
     * 5.内容加密
     * 6.返回字符串
     */
    public static String AESEncode(String content){
        try {
            SecretKey key= initKeyForAES(salt);
            //1.根据指定算法AES自成密码器
            Cipher cipher=Cipher.getInstance("AES");
            //2.初始化密码器，第一个参数为加密(Encrypt_mode)或者解密解密(Decrypt_mode)操作，第二个参数为使用的KEY
            cipher.init(Cipher.ENCRYPT_MODE, key);
            //3.获取加密内容的字节数组(这里要设置为utf-8)不然内容中如果有中文和英文混合中文就会解密为乱码
            byte [] byte_encode=content.getBytes("utf-8");
            //4.根据密码器的初始化方式--加密：将数据加密
            byte [] byte_AES=cipher.doFinal(byte_encode);
            String AES_encode=new String(new BASE64Encoder().encode(byte_AES));
            //5.将字符串返回
            return AES_encode;
        } catch (Exception e) {
            e.printStackTrace();
        }

        //如果有错就返加nulll
        return null;
    }
    /*
     * 解密
     * 解密过程：
     * 1.同加密1-4步
     * 2.将加密后的字符串反纺成byte[]数组
     * 3.将加密内容解密
     */
    public static String AESDncode(String content){

        try {

            SecretKey key=initKeyForAES(salt);
            //1.根据指定算法AES自成密码器
            Cipher cipher=Cipher.getInstance("AES");
            //2.初始化密码器，第一个参数为加密(Encrypt_mode)或者解密(Decrypt_mode)操作，第二个参数为使用的KEY
            cipher.init(Cipher.DECRYPT_MODE, key);
            //3.将加密并编码后的内容解码成字节数组
            byte [] byte_content= new BASE64Decoder().decodeBuffer(content);
            /*
             * 解密
             */
            byte [] byte_decode=cipher.doFinal(byte_content);
            String AES_decode=new String(byte_decode,"utf-8");
            return AES_decode;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
    private static SecretKey initKeyForAES(String key) throws NoSuchAlgorithmException {
        if (null == key || key.length() == 0) {
            throw new NullPointerException("key not is null");
        }
        SecretKeySpec key2 = null;
        SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
        random.setSeed(key.getBytes());
        try {
            KeyGenerator kgen = KeyGenerator.getInstance("AES");
            kgen.init(128, random);
            SecretKey secretKey = kgen.generateKey();
            byte[] enCodeFormat = secretKey.getEncoded();
            key2 = new SecretKeySpec(enCodeFormat, "AES");
        } catch (NoSuchAlgorithmException ex) {
            throw new NoSuchAlgorithmException();
        }
        return key2;
    }

    public static void main(String[] arg){

        System.out.println(AESDncode(
                "dAK4PC0teAbFiQL7mQFoTW0hjZPgX6TD4QVQLKhbqQ1ueYjOlEDDpWLrpnsdF2H2vXbMp95XVR2D\n" +
                "njMbwrvyHmMSD1++CZWwF0B9s7cyOJHxGYTjU/g2tbSZJRV6NXPD5MFqNoaVNq7E+Uqexza2yAm/\n" +
                "/EVy7EgJz7fyDcUB5x88F41CjBwvQmGXryYe1CVIPF7Z4HMvJgrlbSCT+9EmpKvNQyyL+fPZ31fT\n" +
                "MeSZDjE="));
//                        "dAK4PC0teAbFiQL7mQFoTW0hjZPgX6TD4QVQLKhbqQ1ueYjOlEDDpWLrpnsdF2H2vXbMp95XVR2D\n" +
//                "njMbwrvyHmMSD1  CZWwF0B9s7cyOJHxGYTjU/g2tbSZJRV6NXPD5MFqNoaVNq7E Uqexza2yAm/\n" +
//                "/EVy7EgJz7fyDcUB5x88F41CjBwvQmGXryYe1CVIPF7Z4HMvJgrlbSCT 9EmpKvNQyyL fPZ31fT\n" +
//                "MeSZDjE="
    }
}
